

use axum::response::IntoResponse;
use http::StatusCode;
use shq_common::prelude::AuthType;

#[derive(Debug, thiserror::Error)]
pub enum AuthError {
    #[error("认证失败")]
    AuthenticationFailed,
    #[error("Token已过期")]
    TokenExpired,
    #[error("没有{}权限", .0)]
    NotPermitted(AuthType),
    #[error("无效的权限类型:{}", .0)]
    InvalidAuthType(String),
    #[error("内部服务器错误")]
    InternalServerError(String),
}
impl IntoResponse for AuthError {
    fn into_response(self) -> axum::response::Response {
        let (status_code, message) = self.into();
        (status_code, message).into_response()
    }
}
// 在错误转换中返回不同状态码
impl Into<(StatusCode, String)> for AuthError {
    fn into(self) -> (StatusCode, String) {
        match self {
            AuthError::AuthenticationFailed => (StatusCode::UNAUTHORIZED, "认证失败".to_owned()),
            AuthError::TokenExpired => (StatusCode::UNAUTHORIZED, "Token已过期".to_owned()),
            AuthError::InternalServerError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, format!("后台错误:{msg}")),
            AuthError::NotPermitted(auth_type) => (StatusCode::FORBIDDEN, format!("没有{auth_type}权限")),
            AuthError::InvalidAuthType(auth_type) => (StatusCode::FORBIDDEN, format!("无效的权限类型:{auth_type}")),
        }
    }
}

